package com.yy.jhl.util;
import com.yy.common.commonutils.FileUtil;
import com.yy.jhl.app.AppConstant;

import java.io.File;


/**
 *
 * Created by Ellis.Li on 2017/5/9.
 */
public class Configuration {
    private static final String TAG = Configuration.class.getSimpleName();
    private static String ROOT_PATH = AppConstant.DEFAULT_EXTERNAL_PATH
            + AppConstant.MAIN_PATH;
    /**
     * 初始化应用目录
     */
    public static void initDirectoryPath() {
        if (DeviceUtil.hasSDCard()) {
            ROOT_PATH = getExternalROOTPath();
        } else {
            ROOT_PATH = getInternalROOTPath();
        }
        File dir = new File(ROOT_PATH);
        if (!dir.exists()){ // TODO sd卡没有空间，无法创建的处理
            if(!dir.mkdirs()){
                if(!DeviceUtil.isPathHasSpace(ROOT_PATH)){
                    // 磁盘空间不足，切换到UI线程提示用户提示用户
//                    TimerUtil.execute(new Runnable() {
//                        @Override
//                        public void run() {
//                            Context context = MyApplication.getInstance();
//                            PromptUtil.showToastMessage(
//                                    context.getString(R.string.common_disk_notenough_msg),
//                                    context, true);
//                        }
//                    });
                    return;
                }
            }
        }


        dir.setReadable(true, false);
        dir.setWritable(true, false);
        dir.setExecutable(true, false);

        dir = new File(getInstallDirectoryPath());
        if (!dir.exists())
            dir.mkdir();

        dir = new File(getUserImageDirectoryPath());
        if (!dir.exists())
            dir.mkdir();
    }

    /**
     * @brief 获取应用程序存储根目录目录
     */
    public static String getRootPath() {
        if(!FileUtil.fileIsExists(ROOT_PATH)){
            initDirectoryPath();
        }
        return ROOT_PATH;
    }
    /**
     * @brief 获取图片存储目录
     */
    public static String getUserImageDirectoryPath() {
        return getRootPath() + AppConstant.IMAGE_PATH;
    }
    /**
     * @brief 获取安装包目录
     */
    public static String getInstallDirectoryPath() {
        return getRootPath() + AppConstant.INSTALL_PATH;
    }
    public static String getExternalROOTPath(){
        return DeviceUtil.getExternalStorage() + AppConstant.MAIN_PATH;
    }

    public static String getInternalROOTPath(){
        //return MyApplication.getInstance().getFilesDir() + Constants.MAIN_PATH;
        return DeviceUtil.getInternalFilesDir();
    }
}